package com.put.mybills.barcode;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.hardware.Camera;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.provider.ContactsContract;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.Hashtable;
import java.util.regex.Pattern;


import com.google.zxing.BinaryBitmap;
import com.google.zxing.DecodeHintType;
import com.google.zxing.LuminanceSource;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.Result;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.common.HybridBinarizer;



import com.put.mybills.R;

import comt.put.mybills.zxing.IntentIntegrator;
import comt.put.mybills.zxing.IntentResult;

public final class ScanCodeActivity extends Activity {

  private static final String TAG = ScanCodeActivity.class.getSimpleName();
  private static final String PACKAGE_NAME = "zxing";
  private static final Pattern SEMICOLON = Pattern.compile(";");

  @Override
  public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
   // setContentView(R.layout.test);
   //findViewById(R.id.scan_product).setOnClickListener(scanProduct);
    //findViewById(R.id.scan_qr_code).setOnClickListener(scanQRCode);   
    //findViewById(R.id.linear_test).setBackgroundColor(Color.BLACK);
    Log.d("ZXingTestActivity", PACKAGE_NAME );
    
  }

  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater menuInflater = getMenuInflater();
    menuInflater.inflate(R.menu.main, menu);
    return super.onCreateOptionsMenu(menu);
  }

  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
    if (item.getItemId() == R.id.action_settings) {
      int versionCode;
      String versionName;
      try {
    	
        PackageInfo info = getPackageManager().getPackageInfo(PACKAGE_NAME, 0);
        versionCode = info.versionCode;
        versionName = info.versionName;
      } catch (PackageManager.NameNotFoundException ignored) {
        versionCode = 0;
        versionName = "unknown";
      }
      AlertDialog.Builder builder = new AlertDialog.Builder(this);
      builder.setTitle(
          getString(R.string.app_name) + ' ' + versionName + " (" + versionCode + ')');
      builder.setMessage("ABOUT MESSAGE");
      builder.setPositiveButton("OK", null);
      builder.show();

    }
    return super.onOptionsItemSelected(item);
  }
  
  @Override
  public void onActivityResult(int requestCode, int resultCode, Intent intent) {
    IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
    if (result != null) {
      String contents = result.getContents();
      if (contents != null) {    	
        //showDialog("result_succeeded", result.toString());
      } else {
        //showDialog("result_failed", "result_failed_why");
      }
    }
  }
  


  private final View.OnClickListener scanProduct = new View.OnClickListener() {
    @Override
    public void onClick(View v) {
      IntentIntegrator integrator = new IntentIntegrator(ScanCodeActivity.this);
      integrator.addExtra("SCAN_WIDTH", 800);
      integrator.addExtra("SCAN_HEIGHT", 200);
      integrator.addExtra("RESULT_DISPLAY_DURATION_MS", 3000L);
      integrator.addExtra("PROMPT_MESSAGE", "Custom prompt to scan a product");
      integrator.initiateScan(IntentIntegrator.PRODUCT_CODE_TYPES);
    }
  };

  private final View.OnClickListener scanQRCode = new View.OnClickListener() {
    @Override
    public void onClick(View v) {
      IntentIntegrator integrator = new IntentIntegrator(ScanCodeActivity.this);
      integrator.initiateScan(IntentIntegrator.QR_CODE_TYPES);
    }
  };

  

  private void showDialog(int title, CharSequence message) {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle(title);
    builder.setMessage(message);
    builder.setPositiveButton("OK", null);
    builder.show();
  }


 
  
  


}